package utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

public class FileLoadUtil {
    /**
     * 加载properties文件
     *
     * @param path
     * @return
     */
    public static Properties loadProperties(String path) {
        Properties properties = new Properties();
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(path);
            properties.load(fileReader);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeIO(fileReader);
        }
        return properties;
    }

    /**
     * 加载json格式文件
     *
     * @param path
     * @return
     */
    public static String loadJsonString(String path) {
        StringBuilder builder = new StringBuilder();
        FileReader fileReader = null;
        BufferedReader reader = null;
        try {
            fileReader = new FileReader(path);

            reader = new BufferedReader(fileReader);

            String line;

            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeIO(fileReader,reader);
        }
        return builder.toString();
    }

    /**
     * 关闭文件流
     * @param reader
     */
    public static void closeIO(Reader...reader){
        for (Reader r : reader) {
            if (r != null){
                try {
                    r.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
